package cosw.com.shanghaica;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import butterknife.BindView;
import butterknife.ButterKnife;
import cosw.com.shanghaica.util.Constant;

import com.cosw.sdkShanghaiCA.SHCAProvider;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ContainerListActivity extends ActionBarActivity {

  @BindView(R.id.lv_container)
  ListView lvContainer;

  private SHCAProvider provider;

  private Context mContext;

  List<String> containerList;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_container_list);
    ButterKnife.bind(this);

    mContext = this;

    provider = SHCAProvider.getInstance(this.getApplication());

    containerList = provider.getContainerList();

    final List<Map<String, Object>> itemList = new ArrayList<>();
    for (String aTitle : containerList) {
      Map<String, Object> map = new HashMap<>();
      map.put("title", aTitle);
      itemList.add(map);
    }

    ContainerListViewAdapter adapter = new ContainerListViewAdapter(this, itemList);
    lvContainer.setAdapter(adapter);
    lvContainer.setOnItemClickListener(new OnItemClickListener() {

      @Override
      public void onItemClick(AdapterView<?> parent, View view,
          int position, long id) {

        Intent intent = new Intent(ContainerListActivity.this, ContainerActivity.class);
        intent.putExtra(Constant.EXTRA_KEY_CONTAINER_NAME, containerList.get(position));
        startActivity(intent);
      }
    });
  }

}
